$(document).ready(function(){
	$(".edit-address").click(function(){
		loadForm($(this).attr("cid"));
	});

});

function loadForm(cid){
	var cart = $("#edit-form");
	if (cart.length>0){
			$.ajax({
			url: '/partial/address-form/',
			dataType: 'html',
			data :{cid : cid},
			success: function(html) {
				cart.html(html);
 				$("#registerCountry").trigger("change");
				},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
		}
}



function adjustQuantity(itemId,quantity, success){
	var url = '/pub-shopping/'+itemId+'/order/quantity/';
	var this_ = this;
	var submit = {
			url : url,
			type : "POST",
			data : {'quantity' : quantity},
			dataType : "json",
				success : function(data) {
				if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
					alert (data.message);
					return;
				} else {
 				loadSmallCart();
				reloadCartPrice();
				if (success){
					success.apply(this_,[data]);
				}
				}
			}
		};
	$.ajax(submit);	
}
 


function loadSmallCart(){
	var cart = $("#cart");
	if (cart.length>0){
			$.ajax({
			url: '/partial/small-cart/',
			dataType: 'html',
			success: function(html) {
				cart.html(html);
				},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
		}
 }

function reloadCartPrice(){
	
	var this_ = $(this);
	var cart = $("#cart-total-price");
	if (cart.length>0){
			$.ajax({
			url: '/partial/shopping-cart-price/',
			dataType: 'html',
			success: function(html) {
 				cart.html(html);
				},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
		}
	
//	/partial/shopping-cart-price/
	
	
}


$(document).ready(function(){
	if (cms24k.living()) {
		
		
		
		
		loadSmallCart();
		
		$('.coupon-apply').click(function(){
			
  			var form = $(this).parents("form:first");
 			
			var url = '/pub-shopping/coupon-apply/';
			var submit = {
					url : url,
					type : "POST",
					dataType : "json",
					data : form.serialize(),
 					success : function(data) {
						if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
							alert (data.message);
 						return;
						} else {
							loadSmallCart();
							reloadCartPrice();

							alert ("successfully apply coupon");
 						}
					}
				};
				$.ajax(submit);
 		});
		
		
		$(".re-order").click(function(){
			var id = $(this).attr("oid");
			var url = "/pub-shopping/"+id+"/re-order/";
			var submit = {
							 			url : url,
							 			type : "POST",
							 			dataType : "json",
							 			success : function(data) {
							 				if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
							 	 				alert(data.message);
							 					return;
							 				} else {
								 				loadSmallCart();
												alert('Your request has been processed successfully');
							 				}
							 			}
							 		};
		 		$.ajax(submit);
		});
		
		
		$('.signup').click(function(){
 			var form = $(this).parents("form:first");
 			var confirm = form.find("input[name=confirm]").val();
 			var password = form.find("input[name=password]").val();
 			if (confirm!=password){
 				
 				alert('please check your password');
 				return;
 			}
  			var submit = {
					url : '/pub-member/signup/',
					type : "POST",
					dataType : "json",
					data  : form.serialize(),
					success : function(data) {
						if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
							alert (data.message);
							reloadcapcha();
							return;
						} else {
	 						location.reload();
 						}
					}
				};
				$.ajax(submit);
		});
		
		$('.to-wishlist').click(function(){
 			var productId = $(this).attr('proid');
			var url = '/pub-shopping/'+productId+'/wishlist/add/';
			var submit = {
					url : url,
					type : "POST",
					dataType : "json",
 					success : function(data) {
						if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
							alert (data.message);
 						return;
						} else {
							var size = 	parseInt($("#show-wish-size").attr("wsize"))+1;
							$("#show-wish-size").attr("wsize",size);
							$("#wish-span").html(size);
							alert ("successfully added into your wish list");
 						}
					}
				};
				$.ajax(submit);
			
		});
		
		$('.to-cart').click(function(){
 			var productId = $(this).attr('proid');
 
 			var form = $(this).parents("form:first");
 			
			var url = '/pub-shopping/'+productId+'/order/add/';
			var submit = {
					url : url,
					type : "POST",
					dataType : "json",
					data : form.serialize(),
 					success : function(data) {
						if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
							alert (data.message);
 						return;
						} else {
							loadSmallCart();
							processForm(form);
//							alert ("successfully added into your shopping cart");
 						}
					}
				};
				$.ajax(submit);
			
		});
		
		$('.remove-wishlist').click(function(){
		 
			var productId = $(this).attr('proid');
			var url = '/pub-shopping/'+productId+'/wishlist/delete/';
			var this_ = this;
			var submit = {
					url : url,
					type : "POST",
					dataType : "json",
 					success : function(data) {
						if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
							alert (data.message);
 						return;
						} else {
							var x = $(this_).parents(".wish-frame").first();
 
							x.remove();
							
							var size = 	parseInt($("#show-wish-size").attr("wsize"))-1;
							$("#show-wish-size").attr("wsize",size);
							$("#wish-span").html(size);
							alert ("successfully remove from your wish list");
 						}
					}
				};
				$.ajax(submit);
			
		});
		
		
		$(document).delegate('.remove-cart','click',function(){
  			var productId = $(this).attr('itemId');
			var url = '/pub-shopping/'+productId+'/order/delete/';
			var this_ = $(this);
			var submit = {
					url : url,
					type : "POST",
					dataType : "json",
 					success : function(data) {
						if (data && data.state && (data.state.indexOf("ERROR") != -1)) {
							alert (data.message);
 						return;
						} else {
							this_.parents(".shop-cart-item").remove();

							loadSmallCart();
							reloadCartPrice();
							
//							location.reload();
 						}
					}
				};
				$.ajax(submit);			
			
		});
	 
		
		
		
		
		
	}
	});